import 'package:flutter/material.dart';
import 'package:vant/src/routes.dart';
import 'package:vant/src/sections/404_section.dart';
import 'package:vant/src/widgets/widgets.dart';

class VanDocApp extends StatefulWidget {
  const VanDocApp({super.key});

  static switchBrightness(BuildContext context) => VanDocSwitchTheme.switchBrightness(context);

  @override
  State<VanDocApp> createState() => VanDocAppState();
}

class VanDocAppState extends State<VanDocApp> with WidgetsBindingObserver {
  Brightness? _brightness;

  @override
  void initState() {
    WidgetsBinding.instance.addObserver(this);
    super.initState();
  }

  @override
  void dispose() {
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }

  @override
  void didChangePlatformBrightness() {
    _brightness = MediaQuery.of(context).platformBrightness;
    switchBrightness(context);
  }

  switchBrightness(BuildContext context) {
    setState(() {
      _brightness = _brightness == Brightness.dark ? Brightness.light : Brightness.dark;
    });
  }

  @override
  Widget build(BuildContext context) {
    _brightness = _brightness ?? MediaQuery.of(context).platformBrightness;
    final theme = Theme.of(context);
    return VanDocSwitchTheme(
      app: this,
      child: MaterialApp(
        title: 'Flutter Vant 4',
        initialRoute: "/",
        debugShowCheckedModeBanner: false,
        onUnknownRoute: (settings) {
          return MaterialPageRoute(builder: (context) => NotFoundView(title: settings.name!.substring(1), route: settings.name ?? ""));
        },
        // themeMode: ThemeMode.system,
        theme: theme.copyWith(brightness: _brightness ?? theme.brightness),
        builder: (context, child) {
          final theme = Theme.of(context);

          final dark = _brightness == Brightness.dark;
          return Theme(
            data: theme.copyWith(
              brightness: _brightness,
              primaryColor: kVanDocBackground.get(context),
              colorScheme: theme.colorScheme.copyWith(
                background: kVanDocBackground.get(context),
              ),
              appBarTheme: theme.appBarTheme.copyWith(
                backgroundColor: kVanDocBackground3.get(context),
                foregroundColor: null,
                elevation: 0,
                toolbarHeight: 56,
                iconTheme: IconThemeData(
                  size: 16,
                  color: kVanDocTextColor2.get(context),
                ),
                titleTextStyle: TextStyle(
                  color: kVanDocTextColor2.get(context),
                  fontSize: 17,
                  fontWeight: FontWeight.w600,
                ),
              ),
              scaffoldBackgroundColor: dark ? kVanDocBlack.get(context) : kVanDocGray1.get(context),
            ),
            child: DefaultTextStyle.merge(
              // style: GoogleFonts.inconsolata(fontSize: 16, color: kVanDocTextColor2.get(context), fontWeight: FontWeight.w400),
              style: TextStyle(fontSize: 16, color: kVanDocTextColor2.get(context), fontWeight: FontWeight.w400),
              child: child!,
            ),
          );
        },
        routes: routes,
      ),
    );
  }
}

class VanDocSwitchTheme extends InheritedWidget {
  final VanDocAppState app;

  static VanDocAppState? of(BuildContext context) {
    return context.dependOnInheritedWidgetOfExactType<VanDocSwitchTheme>()?.app;
  }

  static switchBrightness(BuildContext context) => of(context)?.switchBrightness(context);

  const VanDocSwitchTheme({super.key, required this.app, required super.child});

  @override
  bool updateShouldNotify(covariant VanDocSwitchTheme oldWidget) => oldWidget.child != child;
}
